package site.fantasycube.blog.config.mybatisPlus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

/**
 * @author 幻&乐
 * @Date 2021/12/25
 * 描述 mybatis plus  自动填充内容
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {


    /**
     * 插入时自动填充内容
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Long node = UUID.randomUUID().node();
        Date addDate = new Date();
        metaObject.setValue("id",node);
        metaObject.setValue("addDate",addDate);
    }

    /**
     * 修改时自动填充内容
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        Date updDate = new Date();
        //自动填充修改时间
        metaObject.setValue("updDate",updDate);
    }
}
